package com.hrm.salarys.controller;

import com.hrm.common.controller.BaseController;
import com.hrm.common.entity.Result;
import com.hrm.common.entity.ResultCode;
import com.hrm.domain.salarys.SalaryArchive;
import com.hrm.domain.salarys.SalaryArchiveDetail;
import com.hrm.salarys.service.ArchiveService;
import com.hrm.salarys.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/salarys")
public class ArchiveController extends BaseController {

    @Autowired
    private SalaryService salaryService;
    @Autowired
    private ArchiveService archiveService;

    //制作薪资报表
    //url参数：201907、optype=1则是新制作的报表，其他：查询已归档的报表数据
    @RequestMapping(value = "/reports/{yearMonth}", method = RequestMethod.POST)
    public Result getReports(@PathVariable  String yearMonth, int opType) {
        List<SalaryArchiveDetail> list = new ArrayList<>();
        //1、判断opType参数
        if (opType == 1){
            //自己构造报表数据
            list = archiveService.getReports(yearMonth,companyId);
        }else {
            //查询归档历史表（查主表id，再去查明细表）
            SalaryArchive salaryArchive = archiveService.findSalaryArchive(yearMonth,companyId);
            if (salaryArchive != null){
                list = archiveService.findSalaryArchiveDetail(salaryArchive.getId());
            }
        }
        return new Result(ResultCode.SUCCESS,list);
    }

    //归档某月薪资报表
    //url参数：201907
    @RequestMapping(value = "/archive/reports/{yearMonth}", method = RequestMethod.POST)
    public Result archiveReports(@PathVariable  String yearMonth) {
        List<SalaryArchiveDetail> list = new ArrayList<>();
        SalaryArchive salaryArchive = archiveService.findSalaryArchive(yearMonth,companyId);
        if (salaryArchive != null){
            list = archiveService.findSalaryArchiveDetail(salaryArchive.getId());
        }
        return new Result(ResultCode.SUCCESS,list);
    }

    //查询所有的历史薪资报表（总表）
    //url参数：201907
    @RequestMapping(value = "/historys/reports/{year}", method = RequestMethod.POST)
    public Result historysReports(@PathVariable  String year) {
        List<SalaryArchive> list = archiveService.findAllSalaryArchive(companyId);
        return new Result(ResultCode.SUCCESS,list);
    }

    //查询某年月下的历史薪资报表细节（detail表）
    //url参数：salaryArchive的id
    @RequestMapping(value = "/historys/reports/{id}", method = RequestMethod.POST)
    public Result historysReportsOne(@PathVariable  String id) {
        List<SalaryArchiveDetail> list = archiveService.findSalaryArchiveDetail(id);
        return new Result(ResultCode.SUCCESS,list);
    }

}
